package com.android.pI;

import java.io.IOException;

import com.android.pI.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MenuActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		DataBaseHelper dbHelper = new DataBaseHelper(this);
		try {
			dbHelper.createDataBase();
			dbHelper.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setContentView(R.layout.menu);

		ListView menuList = (ListView) findViewById(R.id.ListView_Menu);

		String[] items = { getResources().getString(R.string.menu_1),
				getResources().getString(R.string.menu_2),
				getResources().getString(R.string.menu_3)};
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				R.layout.menu_item, items);
		menuList.setAdapter(adapt);

		menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View itemClicked,
					int position, long id) {

				// Note: if the list was built "by hand" the id could be used.
				// As-is, though, each item has the same id

				TextView textView = (TextView) itemClicked;
				String strText = textView.getText().toString();

				if (strText.equalsIgnoreCase(getResources().getString(
						R.string.menu_1))) {
					// Launch the Game Activity
					startActivity(new Intent(MenuActivity.this,
							SelectWkActivity.class));
				}
				if (strText.equalsIgnoreCase(getResources().getString(
						R.string.menu_3))) {
					startActivity(new Intent(MenuActivity.this,
							ContentProvider.class));
				}
				if (strText.equalsIgnoreCase(getResources().getString(
						R.string.menu_2))) {
					startActivity(new Intent(MenuActivity.this,
							LogActivity.class));
				}
			}
		});

	}
	 // /////// Menu Stuff

	  @Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    getMenuInflater().inflate(R.menu.menui, menu);
	    return true;
	  }

	  @Override
	  public boolean onOptionsItemSelected(MenuItem item) {

	    switch (item.getItemId()) {
	    case R.id.itemServiceStart:
	      startService(new Intent(this, UpdaterService.class));
	      break;
	    case R.id.itemServiceStop:
	      stopService(new Intent(this, UpdaterService.class));
	      break;
	    }

	    return true;
	  }
}
